home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / rasterops / windowSnap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.2 KB  |  185 lines

  1. /*
  2.  * Copyright 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* windowSnap.c
  19.  *    Type "windowSnap <imageFileName>".  When the leftmouse 
  20.  *    button is pressed the scene in the window is captured,
  21.  *    and the pixel data is written into a file.  
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <math.h>
  28. #include <stdio.h>
  29. #include "rgbImageFile.h"    /* should be in ../../include */
  30.  
  31. /*  Function Prototypes  */
  32.  
  33. GLvoid  initgfx( GLvoid );
  34. GLvoid  keyboard( GLubyte, GLint, GLint );
  35. GLvoid  mouse( GLint, GLint, GLint, GLint );
  36. GLvoid  drawScene( GLvoid );
  37. GLvoid  reshape( GLsizei, GLsizei );
  38.  
  39. GLvoid    grabImage( void );
  40.  
  41. void    printHelp( char * );
  42.  
  43. /* Global Variables */
  44.  
  45. static char    *imageFileName;
  46. static GLsizei    winWidth, winHeight;
  47.  
  48. GLvoid
  49. main ( int argc, char *argv[] )
  50. {
  51.     GLsizei width, height;
  52.  
  53.     glutInit( &argc,  argv );
  54.  
  55.     if (argc < 2) {
  56.         fprintf(stderr, "usage:  windowSnap <imageFileName>\n");
  57.         imageFileName = "windowSnap.rgb";
  58.     } else 
  59.         imageFileName = argv[1];
  60.     fprintf(stdout, "\n\nImage will be saved in %s\n", 
  61.         imageFileName);
  62.  
  63.     width = glutGet(GLUT_SCREEN_WIDTH); 
  64.     height = glutGet(GLUT_SCREEN_HEIGHT);
  65.     winWidth = width / 2;
  66.     winHeight = height / 2;
  67.     glutInitWindowPosition( width/4, height/4 ); 
  68.     glutInitWindowSize( winWidth, winHeight);
  69.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
  70.     glutCreateWindow( argv[0] );
  71.  
  72.     initgfx();
  73.  
  74.     glutReshapeFunc( reshape );
  75.     glutMouseFunc( mouse );
  76.     glutDisplayFunc(drawScene);
  77.     
  78.     printHelp( argv[0] );
  79.  
  80.     glutMainLoop();
  81. }
  82.  
  83. GLvoid
  84. printHelp( char *progname )
  85. {
  86.     fprintf(stdout, "\n%s, reads pixels from window and saves \n"
  87.         "them in a file when the Left Mousebutton is released\n\n"
  88.         "Left Mousebutton, up        - snapshot of window\n\n",
  89.         progname);
  90. }
  91.  
  92. GLvoid
  93. initgfx( void )
  94. {
  95.     GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
  96.     GLfloat mat_shininess[] = { 10.0 };
  97.  
  98.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  99.  
  100.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  101.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  102.  
  103.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  104.     glEnable(GL_LIGHT0);
  105.  
  106.     glEnable(GL_LIGHTING);
  107.  
  108.     glClearColor( 0, 0, 0, 1 );
  109.  
  110.     glEnable( GL_DEPTH_TEST );
  111. }
  112.  
  113. GLvoid 
  114. mouse( GLint button, GLint state, GLint x, GLint y )
  115. {
  116.     switch (button) {
  117.     case GLUT_LEFT_BUTTON:
  118.         if (state == GLUT_DOWN) {
  119.             grabImage();
  120.         }
  121.         break;
  122.     }
  123. }
  124.  
  125. GLvoid
  126. reshape( GLsizei width, GLsizei height )
  127. {
  128.     GLdouble    aspect;
  129.  
  130.     glViewport( 0, 0, width, height );
  131.  
  132.     winWidth = width;
  133.     winHeight = height;
  134.  
  135.     aspect = (GLdouble) width / (GLdouble) height;
  136.  
  137.     glMatrixMode( GL_PROJECTION );
  138.     glLoadIdentity();
  139.     gluPerspective( 45.0, aspect, 3.0, 13.0 );
  140.     glMatrixMode( GL_MODELVIEW );
  141.     glLoadIdentity();
  142.     glTranslatef( 0.0, 0.0, -8.0 ); 
  143. }
  144.  
  145. GLvoid
  146. grabImage( void )
  147. {
  148.     GLuint    *image;
  149.  
  150.     /* allocate space for the pixel data we're going to read 
  151.      * each pixel consists of four 1-byte components (R, G, B and A) */
  152.     image = (GLuint *) malloc( winWidth * winHeight * 4 );
  153.  
  154.     glReadPixels( 0, 0, winWidth, winHeight, GL_RGBA, GL_UNSIGNED_BYTE, 
  155.             image );
  156.  
  157.     rgbWriteImageFile( imageFileName, winWidth, winHeight, image );
  158.     
  159.     exit(0);
  160. }
  161.  
  162. GLvoid
  163. drawScene( GLvoid )
  164. {
  165.     int i,  slices = 8;
  166.     
  167.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  168.     
  169.     glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
  170.     glEnable( GL_COLOR_MATERIAL );
  171.     for ( i = 0; i < slices; i++ )
  172.     {
  173.         glColor3f( i/10.0, i/10.0, 1.0 - i/10.0 ); 
  174.         glPushMatrix(); 
  175.             glRotatef( i * 360.0/slices, 0, 0, 1 );
  176.             glTranslatef( 1.5, 0.0, 0.0 );
  177.             glRotatef( i * 360.0/slices, 0, 1, 0 );
  178.             glutSolidTorus( 0.25, 0.75, 8, 16 );
  179.         glPopMatrix();
  180.     }
  181.     glDisable( GL_COLOR_MATERIAL );
  182.     glFlush();
  183. }
  184.  
  185.